package com.ruoyi.huanghefd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.huanghefd.domain.ApplysheetPoweroffon;
import com.ruoyi.huanghefd.domain.Makesheet;
import com.ruoyi.huanghefd.domain.Worksheet;
import com.ruoyi.huanghefd.service.ApplysheetPoweroffonService;
import com.ruoyi.huanghefd.mapper.ApplysheetPoweroffonMapper;
import com.ruoyi.huanghefd.utils.PageBean;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 *
 */
@Service
public class ApplysheetPoweroffonServiceImpl extends ServiceImpl<ApplysheetPoweroffonMapper, ApplysheetPoweroffon>
    implements ApplysheetPoweroffonService{

    private static final Logger log = LoggerFactory.getLogger(ApplysheetPoweroffonServiceImpl.class);

    @Resource
    ApplysheetPoweroffonMapper applysheetPoweroffonMapper;

    @Override
    public AjaxResult addApplysheetPoweroffon(ApplysheetPoweroffon applysheetPoweroffon) {

        int result = 0;
        try {
            //停送电申请单主键
            String strWsId = UUID.randomUUID().toString().replace("-", "");

            String strWsCode =this.getGenerateOrder(applysheetPoweroffon.getApType());
            log.info("生成停送电申请单票号：" + strWsCode);
            applysheetPoweroffon.setApCode(strWsCode);
            applysheetPoweroffon.setApId(strWsId);

            //插入工作票主表
            log.info("新增停送电申请单-开始>>" + applysheetPoweroffon);
            result = applysheetPoweroffonMapper.insertAll(applysheetPoweroffon);
            log.info("新增停送电申请单-结束<<" + result);

            return AjaxResult.success(strWsCode);
        } catch (Exception e) {
            return AjaxResult.error("停送电申请单新增失败", e);
        }
    }

    @Override
    public AjaxResult delApplysheetPoweroffon(String strApCode) {
        int result = 0;
        try {
            log.info("删除停送电申请单-开始>>");
            result = applysheetPoweroffonMapper.delByApId(strApCode);
            log.info("删除停送电申请单-结束<<"+result);

            return AjaxResult.success(strApCode);
        } catch (Exception e) {
            return AjaxResult.error("停送电申请单删除失败", e);
        }
    }

    @Override
    public AjaxResult uptApplysheetPoweroffon(ApplysheetPoweroffon applysheetPoweroffon) {
        int result = 0;
        try {
            log.info("修改停送电申请单-开始>>");
            result = applysheetPoweroffonMapper.updateByApId(applysheetPoweroffon);
            log.info("修改停送电申请单-结束<<"+result);

            return AjaxResult.success(applysheetPoweroffon.getApCode());
        } catch (Exception e) {
            return AjaxResult.error("停送电申请单修改失败", e);
        }
    }

    @Override
    public AjaxResult queryApplysheetPoweroffons(String strApCode, String strApState, String strUserId, String strMailType) {
        try {
            List<ApplysheetPoweroffon> applysheetPoweroffonList = applysheetPoweroffonMapper.queryApplysheetPoweroffonList(strApCode,strApState,strUserId,strMailType);
            if (CollectionUtils.isEmpty(applysheetPoweroffonList)) {
                return AjaxResult.success("停送电申请单查询无数据",null);
            }
            return AjaxResult.success("停送电申请单查询成功",applysheetPoweroffonList);
        } catch (Exception e) {
            return AjaxResult.warn("停送电申请单查询失败",e);
        }
    }

    @Override
    public PageBean queryApplysheetPoweroffonsByPage(String strApCode, String strApState, String strUserId, String strMailType, int pageNum, int pageSize) {
        PageBean pageBean = new PageBean();
        if (pageNum > 0 && pageSize > 0) {
            int totalRecord = applysheetPoweroffonMapper.getApplysheetPoweroffonNum(strApCode, strApState, strUserId, strMailType);
            pageBean = new PageBean(pageNum, pageSize, totalRecord);

            //从该行开始拿数据
            int startIndex = (pageNum - 1) * pageSize;
            pageBean.setStartIndex(startIndex);
            pageBean.setCode(200);

            //数据集合
            List<ApplysheetPoweroffon> applysheetPoweroffonList = applysheetPoweroffonMapper.queryApplysheetPoweroffonListByPage(strApCode, strApState, strUserId, strMailType,startIndex, pageSize);
            pageBean.setList(applysheetPoweroffonList);
            pageBean.setSuccessMsg("停送电申请单查询成功！");
        } else {
            pageBean.setCode(500);
            pageBean.setFailMsg("停送电申请单查询失败！");
        }
        return pageBean;
    }

    /**
     * @Author gnplum
     * @Description //停送电票号
     * @Date 14:56 2023/11/12
     * @param Type
     * @return java.lang.String
     **/
    private String getGenerateOrder(String Type) {

        log.info("获取票编号=" + Type);
        String strWsCode = "";
        int order = 0;
        try {

            order = applysheetPoweroffonMapper.getApCodeNum(Type);
            log.info("order=" + order);
            //DQ1202311110001
            strWsCode = Type + new SimpleDateFormat("yyyyMMdd").format(new Date()) + String.format("%03d", order);

            log.info("strWsCode=" + strWsCode);

        } catch (Exception e) {
            strWsCode = "";
            //e.printStackTrace();
            throw  e;
            //log.error(e.getCause()+e.getMessage());
        }
        return strWsCode;
    }
}




